home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / SYS / s / templates.wrx < prev    next >
Text File  |  1996-09-26  |  9KB  |  394 lines

  1. /* Fill in the C-templates
  2.  *      Header.tpl
  3.  *      AutoDoc.tpl
  4.  *
  5.  * written 18/09/1993 by Lars Hanke
  6.  * © 1993 by MGR-Software, Asgard
  7.  * contact: mgr@asgard.bo.open.de
  8.  *
  9.  * Header.tpl:
  10.  *      Pass HEADER as argument, the rest is done right away
  11.  *
  12.  *      Fills in dates, filename and the project name taken from
  13.  *      the parental drawer's name
  14.  *
  15.  * AutoDoc.tpl:
  16.  *      Pass IDOC for an autodoc marked internal or GDOC for a
  17.  *      standard autodoc template
  18.  *
  19.  *      The autodoc template must be followed by a strict ANSI
  20.  *      function declaration. Function names, prototypes and
  21.  *      synopsis are guessed from that.
  22.  *      By now however any brace inside the definition won't
  23.  *      be handled following that function pointers won't produce
  24.  *      correct prototypes. Furthermore the whole declaration must
  25.  *      rest in a single line
  26.  *
  27.  * History:
  28.  *
  29.  * 07/02/94   made it accept (void *) as functions with a return
  30.  *            value. Rewrote blkrep according to the new WRITE
  31.  *            blockreplace features.
  32.  * 13/11/93   added the static keyword
  33.  * 17/10/93   added the rest of the declaration keywords such as
  34.  *            __asm, __saveds, __regargs, __stdargs, __a?, __d?,
  35.  *            __inline. Furthermore rewrote the line acquisition to
  36.  *            SetRexxClip to allow infinite line lengths
  37.  *            Finally made it accept type function(void) as a valid
  38.  *            definition. It should now accept anything but function
  39.  *            pointers
  40.  * 23/09/93   introduced some expanded keywords namely signed,
  41.  *            unsigned and register. I know there are more, but
  42.  *            I lack the time complete this
  43.  * 18/09/93   got it running the first time
  44.  */
  45.  
  46. arg tpl_type id
  47.  
  48. IF ~show('P',"WRITE")
  49. THEN DO
  50.  say 'This script does not make sense without the "WRITE" editor !'
  51.  exit 10
  52. END
  53.  
  54. address 'WRITE'
  55. options results
  56.  
  57. 'VERSIONCHECK 3848 "Templates.wrx"'
  58. IF RC~=0 THEN DO
  59.   exit 10
  60. END
  61.  
  62. if (id="") then 'LockWindow _CurrentID'
  63. else 'LockWindow' id
  64.  
  65. if (RC ~= 0)
  66. then do
  67.  'MessageOK "You should have a window!"'
  68.  exit 10
  69. end
  70.  
  71. if(tpl_type="")
  72. then do
  73.   'MessageOK "You must specify the template type!"'
  74.   'LockWindow 0'
  75.   exit 10
  76. end
  77.  
  78. /* Find bounds of the loaded template */
  79. 'Silent'
  80.  
  81. 'GETVAR "_XPOS"'
  82. a.x=RESULT
  83. 'GETVAR "_YPOS"'
  84. a.y=RESULT
  85.  
  86. 'SETVAR "_FindString" "*/"'
  87. 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
  88. 'CURSORRIGHT 1'
  89. 'CURSORRIGHT 1'
  90.  
  91. 'GETVAR "_XPOS"'
  92. b.x=RESULT
  93. 'GETVAR "_YPOS"'
  94. b.y=RESULT
  95.  
  96. /* Get filename and base directory as project name */
  97.  
  98. 'GETVAR "_FILE"'
  99. file=RESULT
  100. 'GETVAR "_FILEPATH"'
  101. proj=RESULT
  102.  
  103. dir=lastpos('/',proj)
  104. if dir = 0 then dir=lastpos(':',proj)
  105. if dir ~= 0 then proj=delstr(proj,1,dir)
  106.  
  107. /* Get relevant dates */
  108.  
  109. year=delstr(date('s'),5)
  110. revdate=date('E')
  111.  
  112. /* Kill the internal flag, if we're doing global autodocs */
  113.  
  114. if (tpl_type = "GDOC") then
  115. do
  116.   'GOTO' a.x+5 a.y
  117.   'DELETE'
  118.   'WRITECHAR' c2d('*')
  119. end
  120.  
  121. /* Setup synopsis and prototype, if we're dealing with autodocs */
  122.  
  123. if ((tpl_type = "GDOC") | (tpl_type = "IDOC")) then
  124. do
  125.  
  126. /* get the line we expect to hold the function declaration */
  127.  
  128.   'GETVAR "_WordDef"'
  129.   w_def=RESULT
  130.   'SETVAR "_WordDef" "0"'
  131.   'GOTO' b.x b.y
  132.   'NEXTWORD'
  133.   'SetRexxClip (WRITETemplateClip) 0 _YPos'
  134.   line=getclip('WRITETemplateClip')
  135.   'SETVAR "_WordDef" "'||w_def||'"'
  136.  
  137. /* get the function name and type */
  138.  
  139.   p=splittype(line)
  140.   if (p=0) then
  141.   do
  142.      'MessageOK "No function declaration found!"'
  143.      call done
  144.   end
  145.   pc = substr(line,p,1)
  146.   if (pc ~= "(") then
  147.   do
  148.      'MessageOK "This is not a function declaration!"'
  149.      call done
  150.   end
  151.  
  152.   funcnam = st.name
  153.   retname = ""
  154.   if (pos("void",st.type) ~= 0) then
  155.   do
  156.      if (pos("*",st.type) = 0) then
  157.      do
  158.         retname = "NONE"
  159.      end
  160.   end
  161.  
  162.   if (retname ~= "NONE") then
  163.   do
  164.      if (pos("int",st.type) ~= 0) then retname = "error"
  165.      else retname = ""
  166.      'GETSTRING "Function' funcnam 'returns" "'||retname||'"'
  167.      retname = RESULT
  168.      synopsis = retname "=" st.name "("
  169.   end
  170.   else synopsis = st.name "("
  171.   proto = st.type st.name "("
  172.  
  173. /* now scan arguments */
  174.  
  175.   inputs=""
  176.   st.help = strip(delstr(line,1,p))
  177.   pce = substr(st.help,1,1)
  178.   if (pce = ")") then           /* check for empty chain */
  179.   do
  180.      synopsis = synopsis ")"
  181.      proto = proto "void )"
  182.      inputs = "NONE"
  183.   end
  184.   else
  185.   do while ((pc = "(") | (pc = ","))
  186.      line = delstr(line,1,p)
  187.      p = splittype(line)
  188.      if (p=0) then
  189.      do
  190.         if(words(st.help)=0 & words(st.type)=1) then
  191.         do
  192.            if(pos("void",st.type) ~= 0) then
  193.            do
  194.               proto = proto st.type
  195.               inputs = "NONE"
  196.               p = gpoint
  197.            end
  198.            else inputs = "##ERROR##"
  199.         end
  200.         else inputs = "##ERROR##"
  201.         if inputs == "##ERROR##" then
  202.         do
  203.            'MessageOK "Incorrect argument definition!\n'||proto'???"'
  204.            call done
  205.         end
  206.      end
  207.      pc = substr(line,p,1)
  208.  
  209.      if inputs ~= "NONE" then
  210.      do
  211.         synopsis = synopsis st.name
  212.         proto = proto st.type
  213.         inputs = inputs||st.name
  214.      end
  215.  
  216.      if (pc ~= ")") then
  217.         if (pc = ",") then
  218.         do
  219.            if inputs == "NONE" then do
  220.               'MessageOK "You cannot chain void stubs:' proto ',???"'
  221.               call done
  222.            end
  223.            synopsis = synopsis||","
  224.            proto = proto||","
  225.            inputs = inputs||";"
  226.         end
  227.         else
  228.         do
  229.            'MessageOK "Incorrect delemiter in arguments:' pc||'\n'||proto'???"'
  230.            call done
  231.         end
  232.      else
  233.      do
  234.         synopsis = synopsis ")"
  235.         proto = proto ")"
  236.      end
  237.   end
  238. end
  239.  
  240. 'GETVAR "_CaseSense"'
  241. case=RESULT
  242. 'SETVAR "_CaseSense" "0"'
  243.  
  244. call blkrep "$(Project)",proj
  245. call blkrep "$(File)",file
  246. call blkrep "$(Date)",revdate
  247. call blkrep "$(Year)",year
  248.  
  249. /* Now the autodoc specifiy stuff */
  250.  
  251. if ((tpl_type = "GDOC") | (tpl_type = "IDOC")) then
  252. do
  253.   call blkrep "$(Function)",funcnam
  254.   call blkrep "$(Synopsis)",synopsis
  255.   call blkrep "$(Prototype)",proto
  256.   call blkrep "$(Returns)",retname
  257.  
  258. /* Finally build the input list */
  259.  
  260.   'GOTO' a.x a.y
  261.   'SETVAR "_FindString" "$(Inputs)"'
  262.   'FIND @CURSOR @CURSOR @EOT @EOT {@SILENT}'
  263.   if(RC = 0) then
  264.   do
  265.      'GETVAR "_XPOS"'
  266.      x=RESULT
  267.      'GETVAR "_YPOS"'
  268.      y=RESULT
  269.      if ((y<b.y) | ((y=b.y) & (x~>b.x))) then
  270.      do
  271.         'DeleteLine'
  272.         do until (l ~> 0)
  273.            'UnDelLine'
  274.            'Goto @SOL @SOL'
  275.            p = pos(';',inputs)
  276.            if (p = 0) then
  277.            do
  278.               line = inputs
  279.               inputs = ""
  280.            end
  281.            else do
  282.               line = left(inputs,p-1)
  283.               inputs = delstr(inputs,1,p)
  284.            end
  285.            l =length(inputs)
  286.            'SETVAR "_ReplaceString" "'||line||'"'
  287.            'REPLACE @CURSOR @CURSOR @EOT @EOT {@SILENT @NOREQ}'
  288.            'CURSORDOWN 1'
  289.         end
  290.      end
  291.   end
  292. end
  293.  
  294. 'SETVAR "_CaseSense" "'||case||'"'
  295. call done
  296.  
  297. /* replace all in block */
  298.  
  299. blkrep: procedure EXPOSE a. b.
  300.         parse arg old, new
  301.  
  302.         'SETVAR "_FindString" "'||old||'"'
  303.         'SETVAR "_ReplaceString" "'||new||'"'
  304.         'REPLACE' a.x a.y b.x b.y '{@SILENT @NOREQ @ALL}'
  305.  
  306. return
  307.  
  308. /* gets constructs like:
  309.      st.line="  struct foo *  bar[] , ..."
  310.    and produces
  311.      st.type="struct foo **"
  312.      st.name="bar"
  313.    and returns the position of the comma;
  314.    it accepts commas and parenthesa as delemiters. */
  315.  
  316. splittype: procedure EXPOSE st. gpoint
  317.   parse arg st.line
  318.  
  319.   st.help=st.line
  320.   c=pos('(',st.help)
  321.   if c=0 then c=pos(',',st.help)
  322.   if c=0 then c=pos(')',st.help)
  323.   if c=0 then
  324.   do
  325.      gpoint = 0
  326.      return 0
  327.   end
  328.  
  329.   gpoint = c
  330.   st.help=delstr(st.help,c)
  331.   st.help=strip(st.help)
  332.  
  333. /* collect prefixes */
  334.  
  335.   st.run=1
  336.   st.type = ""
  337.   do while (st.run > 0 )
  338.      st.item=subword(st.help,1,1)
  339.      select
  340.         when st.item == "struct" then st.run=1
  341.         when st.item == "unsigned" then st.run=2
  342.         when st.item == "register" then st.run=3
  343.         when st.item == "signed" then st.run=4
  344.         when st.item == "__saveds" then st.run=5
  345.         when st.item == "__asm" then st.run=6
  346.         when left(st.item ,3) == "__a" then do
  347.            if length(st.item = 4) & datatype(right(st.item,1),'w') then st.run = 7
  348.         end
  349.         when left(st.item ,3) == "__d" then do
  350.            if length(st.item = 4) & datatype(right(st.item,1),'w') then st.run = 8
  351.         end
  352.         when st.item == "__regargs" then st.run = 9
  353.         when st.item == "__stdargs" then st.run = 10
  354.         when st.item == "__inline"  then st.run = 11
  355.         when st.item == "const"     then st.run = 12
  356.         when st.item == "static"    then st.run = 13
  357.         otherwise st.run=0
  358.      end
  359.  
  360. /* the last entry is no keyword, but an arbitrary type that we add here in the
  361.    same line with keywords. However only keywords will be removed from st.help */
  362.  
  363.      st.type = st.type st.item
  364.      if(st.run > 0) then st.help=strip(delword(st.help,1,1))
  365.   end
  366.  
  367. /* collect pointer asterisks and arrays */
  368.  
  369.   st.help=strip(delword(st.help,1,1))
  370.   a=0
  371.   do while left(st.help,1)="*"
  372.    a=a+1
  373.    st.help=strip(delstr(st.help,1,1))
  374.   end
  375.   do while right(st.help,2)="[]"
  376.    a=a+1
  377.    st.help=trim(delstr(st.help,length(st.help)-1))
  378.   end
  379.  
  380.   if (a ~= 0) then st.type=st.type copies("*",a)
  381.   st.type=strip(st.type)
  382.   if words(st.help)=1 then st.name=st.help
  383.   else return 0
  384.  
  385. return c
  386.  
  387. /* exit this program */
  388.  
  389. done: procedure
  390.  
  391.   'Window 0 0 0 0'
  392.   'LockWindow 0'
  393.   exit 0
  394.